package cn.exrick.xboot.modules.commonLink.controller;

import cn.exrick.xboot.common.utils.PageUtil;
import cn.exrick.xboot.common.utils.ResultUtil;
import cn.exrick.xboot.common.vo.PageVo;
import cn.exrick.xboot.common.vo.Result;
import cn.exrick.xboot.modules.commonLink.entity.CommonLink;
import cn.exrick.xboot.modules.commonLink.service.ICommonLinkService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Exrick
 */
@Slf4j
@RestController
@Api(description = "常用链接管理接口")
@RequestMapping("/xboot/commonLink")
@Transactional
public class CommonLinkController {

    @Autowired
    private ICommonLinkService iCommonLinkService;

    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "通过id获取")
    public Result<CommonLink> get(@PathVariable String id){

        CommonLink commonLink = iCommonLinkService.getById(id);
        return new ResultUtil<CommonLink>().setData(commonLink);
    }

    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ApiOperation(value = "获取全部数据")
    public Result<List<CommonLink>> getAll(){

        List<CommonLink> list = iCommonLinkService.list();
        return new ResultUtil<List<CommonLink>>().setData(list);
    }

    @RequestMapping(value = "/getByPage", method = RequestMethod.GET)
    @ApiOperation(value = "分页获取")
    public Result<IPage<CommonLink>> getByPage(@ModelAttribute PageVo page){

        IPage<CommonLink> data = iCommonLinkService.page(PageUtil.initMpPage(page));
        return new ResultUtil<IPage<CommonLink>>().setData(data);
    }

    @RequestMapping(value = "/insertOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "编辑或更新数据")
    public Result<CommonLink> saveOrUpdate(@ModelAttribute CommonLink commonLink){
        commonLink.setUpdateTime(new Date());
        if(iCommonLinkService.saveOrUpdate(commonLink)){
            return new ResultUtil<CommonLink>().setData(commonLink);
        }
        return new ResultUtil<CommonLink>().setErrorMsg("操作失败");
    }

    @RequestMapping(value = "/delByIds/{ids}", method = RequestMethod.DELETE)
    @ApiOperation(value = "批量通过id删除")
    public Result<Object> delAllByIds(@PathVariable String[] ids){

        for(String id : ids){
            iCommonLinkService.removeById(id);
        }
        return new ResultUtil<Object>().setSuccessMsg("批量通过id删除数据成功");
    }

    @RequestMapping(value = "/selectCommonLinkPageVoMap", method = RequestMethod.GET)
    @ApiOperation(value = "常用链接条件分页获取数据")
    public Result<IPage<CommonLink>> selectCommonLinkPageVoMap(@ModelAttribute Page page, @Param("title") String title, @Param("department") String department) {
        Map<String, Object> map = new HashMap<String, Object>();

        if(title!=null&&!title.equals("")&&title.length()!=0&&!title.trim().isEmpty()){
            map.put("title",title);
        }
        if(department!=null&&!department.equals("")&&department.length()!=0&&!department.trim().isEmpty()){
            map.put("department", department);
        }

        IPage<CommonLink> commonLinkIPage = iCommonLinkService.selectCommonLinkPageVoMap(page, map);
        return new ResultUtil<IPage<CommonLink>>().setData(commonLinkIPage);
    }
    @RequestMapping(value = "/addCommonLink", method = RequestMethod.POST)
    @ApiOperation(value = "新增保存数据")
    public Result<Object> addCommonLink(@ModelAttribute CommonLink commonLink) {
        commonLink.setCreateTime(new Date());
        commonLink.setUpdateTime(new Date());
        boolean save = iCommonLinkService.save(commonLink);
        if(save){
            return new ResultUtil<Object>().setSuccessMsg("添加成功");
        }
        return new ResultUtil<Object>().setErrorMsg("添加失败");
    }
    /*
    @RequestMapping(value = "/addDownloadResources", method = RequestMethod.POST)
    @ApiOperation(value = "新增保存数据")
    public Result<Object> addDownloadResources(@ModelAttribute DownloadResources downloadResources) {
        boolean save = iDownloadResourcesService.save(downloadResources);
        if(save){
            return new ResultUtil<Object>().setSuccessMsg("添加成功");
        }
        return new ResultUtil<Object>().setErrorMsg("添加失败");
    }
    */

}
